//
//  BattleController.h
//  This file is part of D&D Manager
//
//  Created by Matthew Hutton on 16/02/2007.
//
//
//D&D Manager, a D&D Character and battle management program.
//Copyright (C) 2007, Matthew Hutton
//
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program; if not, write to:
//The Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#import <Cocoa/Cocoa.h>
#import "AttackBonus.h"
#import "Damage.h"
#import "WeaponType.h"
#import "CurrentWeapon.h"
#import "ToYesNo.h"
#import "NotRangedWeapon.h"
#import "SpellSubviewController.h"
#import "MetaMagicFeatController.h"
#import "XValue.h"
#import "LevelOrHD.h"
#import "ArmourClass.h"
#import "EnableSpellResistance.h"
#import "EnableAttackRollEdit.h"
#import "EnableSavingThrow.h"
#import "EnableDiceRolls.h"

@interface BattleController : NSWindowController {
	//value transformers
	Damage *damT;
	AttackBonus *attackB;
	WeaponType *wType;
	CurrentWeapon *cWeap;
	ToYesNo *tyn;
	NotRangedWeapon *nRang;
	LevelOrHD *lvlOrHD;
	ArmourClass *ac;
	EnableSpellResistance *enableSR;
	EnableAttackRollEdit *enableARoll;
	EnableSavingThrow *enableSThrow;
	EnableDiceRolls *enableDRolls;
	//start alphabetical.
	//main screen.
	IBOutlet NSTextField *acAdjTF;
	IBOutlet NSButton *addToSurprisedButton;
	IBOutlet NSTabView *battleOptions;
	IBOutlet NSWindow *battleScreen;
	IBOutlet NSImageView *characterPortrait;
	IBOutlet NSButton *coverButton;
	IBOutlet NSButton *flankedButton;
	IBOutlet NSButton *improvedCoverButton;
	IBOutlet NSWindow *infoPanel;
	IBOutlet NSTabView *mainTabs;
	IBOutlet NSButton *moreDetailButton;
	IBOutlet NSButton *moreDetailButton2;
	IBOutlet NSBox *moreInfoBox;
	IBOutlet NSBox *moreInfoBox2;
	IBOutlet NSTabView *optionTabs;
	IBOutlet NSTextField *rangeIncrementLabel;
	IBOutlet NSTextField *rangeIncrementTF;
	IBOutlet NSButton *removeFromSurprisedButton;
	IBOutlet NSTextField *surpriseDCBox;
	NSMutableArray *surprisedCharactersList;
	IBOutlet NSTableView *surprisedCharactersTable;
	IBOutlet NSWindow *surpriseWindow;
	IBOutlet NSTableView *toChooseSurprisedCharsTable;
	IBOutlet NSTextField *turnDescription;
	//end Alphabetcal
	//main window
	//buttons for slowed/shortcuts and enable/disable.
	IBOutlet NSButton *fullAttackB;
	IBOutlet NSButton *fullAttackE;
	IBOutlet NSButton *moveAndAttackB;
	IBOutlet NSButton *moveAndAttackE;
	IBOutlet NSButton *castSpellB;
	IBOutlet NSButton *castSpellM;
	IBOutlet NSButton *castSpellE;
	IBOutlet NSButton *waitW;
	IBOutlet NSButton *waitS;
	IBOutlet NSButton *skipTurnW;
	IBOutlet NSButton *skipTurnS;
	IBOutlet NSButton *attackS;
	IBOutlet NSButton *specialAbilityB;
	IBOutlet NSButton *specialAbilityA;
	IBOutlet NSButton *specialAbilityE;
	IBOutlet NSButton *changeWeaponA;
	IBOutlet NSButton *changeWeaponE;
	IBOutlet NSButton *turnUndeadM;
	IBOutlet NSButton *turnUndeadE;
	IBOutlet NSButton *spellDischargeM;
	IBOutlet NSButton *spellDischargeE;
	//imported.
	NSWindow *mainScreen;
	NSManagedObject *battleStore;
	NSArrayController *mainCharacterController;
	//controllers
	IBOutlet NSArrayController *characterController;
	IBOutlet NSArrayController *skillController;
	IBOutlet NSArrayController *featsController;
	IBOutlet NSArrayController *weaponController;
	//managedObject stuff
	NSManagedObjectContext *mObjC;
	NSManagedObjectModel *model;
	//currently busy etc.
	IBOutlet NSProgressIndicator *currentlyBusy;
	//wait panel
	IBOutlet NSWindow *waitSheet;
	IBOutlet NSTableView *waitTable;
	IBOutlet NSButton *waitUntilSameButton;
	IBOutlet NSButton *waitUntilBeforeButton;
	IBOutlet NSButton *customWaitButton;
	IBOutlet NSTextField *waitBlurb;
	IBOutlet NSTextField *customInitToWaitFor;
	//turn undead.
	IBOutlet NSTableView *turnUndeadTable;
	IBOutlet NSWindow *turnUndeadSheet;
	IBOutlet NSTextField *turnUndeadDamageTotal;
	IBOutlet NSTextField *turnUndeadDamageLeft;
	IBOutlet NSTextField *turnUndeadBlurb;
	IBOutlet NSTextField *turnUndeadMaxHD;
	IBOutlet NSButton *turnUndeadCloseButton;
	//attribute tester.
	IBOutlet NSWindow *testAttributeSheet;
	IBOutlet NSTextField *difficultyClassInBattle;
	IBOutlet NSTextField *testResult;
	IBOutlet NSTableView *testAttributeTable;
	//feats
	IBOutlet NSWindow *changeFeatsSheet;
	IBOutlet NSTextField *featTargetBlurb;
	IBOutlet NSWindow *featTargetSheet;
	IBOutlet NSTableView *featTargetTable;
	IBOutlet NSTableView *featChangeTable;
	IBOutlet NSTextField *xModTF;
	//special abilities
	IBOutlet NSWindow *specialAbilitiesSheet;
	//changing weapons
	IBOutlet NSWindow *changeWeaponsSheet;
	IBOutlet NSTableView *weaponsTable;
	//cast spells
	IBOutlet NSWindow *castSpellSheet;
	IBOutlet NSTableView *spellsToCastTable;
	NSMutableArray *charactersSpellClasses;
	int c1,c2;
	IBOutlet NSTextField *rangeTF;
	IBOutlet NSTextField *durationTF;
	IBOutlet NSTextField *damageLabel;
	IBOutlet NSTextField *damageTF;
	IBOutlet NSTextField *noaTF;
	IBOutlet NSTextField *castingTF;
	IBOutlet NSButton *verbal;
	IBOutlet NSButton *somatic;
	IBOutlet NSButton *material;
	IBOutlet NSButton *focus;
	IBOutlet NSButton *xp;
	IBOutlet NSButton *attackRoll;
	IBOutlet NSButton *dischargable;
	IBOutlet NSTextField *aoeType;
	IBOutlet NSTextField *aoeSizeLabel;
	IBOutlet NSTextField *aoeSize;
	IBOutlet NSTextField *materialTF;
	IBOutlet NSTextView *spellDescription;
	IBOutlet NSTextField *spellToCastBlurb;
	IBOutlet NSButton *toConcentrate;
	IBOutlet NSTextField *metaFeatNamesBox;
	//feats for spells.
	MetaMagicFeatController *mmFeatController;
	IBOutlet NSButton *metaMagicEditButton;
	//spell overwrite
	IBOutlet NSTextField *spellOverwriteBlurb;
	IBOutlet NSTableView *spellOverwriteTable;
	IBOutlet NSWindow *spellOverwritePanel;
	//spell targets.
	IBOutlet NSTextField *aoeInvisibox;
	IBOutlet NSTextField *spellTargetBlurb;
	IBOutlet NSWindow *spellTargetSheet;
	IBOutlet NSTextField *rangeTarTF;
	IBOutlet NSTextField *rangeLabel;
	IBOutlet NSTextField *durationTarTF;
	IBOutlet NSTextField *durationLabel;
	IBOutlet NSTextField *noOfAttacksTF;
	IBOutlet NSTextField *damageTarTF;
	IBOutlet NSTextField *aoeTarSize;
	IBOutlet NSTextField *aoeTarSizeLabel;
	IBOutlet NSTextField *aoeTarType;
	IBOutlet NSTableView *spellTargetTable;
	IBOutlet NSTableView *spellTargetsChooserTable;
	IBOutlet NSButton *addToSpellTargetsButton;
	IBOutlet NSButton *removeFromSpellTargetsButton;
	IBOutlet NSButton *continueFromSpellTargetsButton;
	//attack OO.
	IBOutlet NSTextField *attackOOBlurb;
	IBOutlet NSTableView *attackOOTable;
	IBOutlet NSWindow *attackOOSheet;
	//attack sheet stuff.
	BOOL lastCharParalysed;
	IBOutlet NSTextField *attackBlurb;
	IBOutlet NSWindow *attackSheet;
	IBOutlet NSTextField *maxRangeBlurb;
	IBOutlet NSTextField *attackAdjTF;
	IBOutlet NSTextField *distanceToEnemyLabel;
	IBOutlet NSTextField *distanceToEnemyTF;
	IBOutlet NSButton *attackContinueButton;
	IBOutlet NSTableView *attackTargetTable;
	//spell discharge.
	IBOutlet NSWindow *spellDischargeSheet;
	IBOutlet NSTableView *spellDischargeTable;
	//to refresh spell tables.
	SpellSubviewController *ssController;
	//edit stuff.
	IBOutlet NSButton *criticalHitEdit;
	IBOutlet NSTextField *canEnableCriticalEdit;
	IBOutlet NSButton *hitTargetEdit;
	IBOutlet NSTextField *diceDamageEdit;
	IBOutlet NSTextField *fixedBonusDamageEdit;
	IBOutlet NSTextField *bonusDiceDamageEdit;
	IBOutlet NSTextField *miscBonusEdit;
	IBOutlet NSTextField *totalDamageEdit;
	IBOutlet NSTextField *targetHPLeftEdit;
	IBOutlet NSWindow *editSheet;
	IBOutlet NSButton *arcaneSpellFailureEdit;
	IBOutlet NSButton *concentrationCheckEdit;
	IBOutlet NSButton *spellResistanceEdit;
	IBOutlet NSButton *attackRollEdit;
	IBOutlet NSButton *targetFailedSaveEdit;
	IBOutlet NSTableView *spellTargetTableEdit;
	IBOutlet NSTextField *diceDamageEdit2;
	IBOutlet NSTextField *fixedBonusDamageEdit2;
	IBOutlet NSTextField *miscBonusEdit2;
	IBOutlet NSTextField *totalDamageEdit2;	
	IBOutlet NSTextField *targetHPLeftEdit2;
	IBOutlet NSWindow *editSheet2;
	IBOutlet NSArrayController *spellTargetController;
}
-(id)initWithBattleStore:(NSManagedObject *)battleCont
			   withMainWindow:(NSWindow *)mainWindow
	  withCharacterController:(NSArrayController *)charContMain;
//start alphabetical.
-(IBAction)addToSurprisedList:(id)sender;
-(NSWindow *)battleScreen;
-(IBAction)closePlayerInfoPanel:(id)sender;
-(IBAction)closeSurpriseSheet:(id)sender;
-(NSManagedObjectContext *)managedObjectContext;
-(void)playerInfoClosed:(NSWindow *)panel
			 returnCode:(int)returnCode
			contextInfo:(NSArray *)info;
-(IBAction)removeFromSurprisedList:(id)sender;
-(IBAction)rollListenCheck:(id)sender;
-(IBAction)rollSpotCheck:(id)sender;
-(IBAction)showDicePanel:(id)sender;
-(IBAction)showPlayerInfoPanel:(id)sender;
-(IBAction)showPlayerInfoPanelFromSpellsOrFeats:(id)sender;
//end alphabetical
-(IBAction)showMoreInfoDetail:(id)sender;
-(IBAction)showMoreInfoDetailEOR:(id)sender;
//setup
-(void)battleNewSet;
-(void)previousExitingSheetEnded:(NSWindow *)sheet
					  returnCode:(int)returnCode
					 contextInfo:(void  *)contextInfo;
-(void)battleNewSet2;
-(void)battleContinueSet;
-(void)newAndContinueSetupIsNew:(BOOL)isNew
					 isSummoned:(BOOL)summoned
					activeSpell:(NSManagedObject *)activeSpell;
-(void)processForBattleTheChar:(NSManagedObject *)obj
					isSummoned:(BOOL)summoned
				forActiveSpell:(NSManagedObject *)activeSpell;
-(IBAction)exitBattleMode:(id)sender;
-(NSString *)battleExitSet:(BOOL)deleteOldChars;
-(void)beginningOfRoundSetup;
-(void)setUpBattleDisplayWithMessage:(NSString *)message;
-(NSString *)createBlurbForCharacter:(NSManagedObject *)obj;
-(BOOL)runCurrentAttacks;
-(BOOL)runCurrentSpells;
//button presses.
-(IBAction)fullAttack:(id)sender;
-(IBAction)moveAndAttack:(id)sender;
-(IBAction)move:(id)sender;
-(IBAction)wait:(id)sender;
-(IBAction)skipTurn:(id)sender;
-(IBAction)refocus:(id)sender;
-(IBAction)castSpell:(id)sender;
-(IBAction)turnUndead:(id)sender;
-(IBAction)dischargeSpells:(id)sender;
-(IBAction)changeFeats:(id)sender;
-(IBAction)useSpecialAbility:(id)sender;
-(IBAction)changeWeapon:(id)sender;
-(IBAction)editCharacters:(id)sender;
-(void)editCharactersCode;
-(IBAction)charge:(id)sender;
-(IBAction)testAttribute:(id)sender;
-(NSManagedObject *)currentCharacter;
-(void)castSpellFromMainScreenWithCharacter:(NSManagedObject *)obj
						   withSSController:(SpellSubviewController *)theSsController;
-(void)beginningSheetEnded:(NSWindow *)sheet
				returnCode:(int)code
			   contextInfo:(void *)contextInfo;
//turn undead.
-(IBAction)turnUndeadSheetEnded:(id)sender;
-(void)alertFromTurningDidEnd:(NSWindow *)panel
				   returnCode:(int)returnCode
				  contextInfo:(void *)contextInfo;
//skills.
-(IBAction)testAttributeInBattle:(id)sender;
-(IBAction)closeTestWindow:(id)sender;
//random
-(void)processOptionTabsForTabViewItem:(NSTabViewItem *)tabViewItem;
//waiting
-(IBAction)waitUntilBefore:(id)sender;
-(IBAction)waitUntil:(id)sender;
-(IBAction)waitUntilAfter:(id)sender;
-(IBAction)waitUntilCustomInitiative:(id)sender;
-(IBAction)cancelWaitWindow:(id)sender;
-(void)processWaitButtons;
//feats
-(IBAction)xModifierChanged:(id)sender;
-(IBAction)changeFeatSheetEnded:(id)sender;
-(IBAction)specialAbilitySheetEnded:(id)sender;
-(void)displayFeatTargetSheetByChar:(NSManagedObject *)obj
							forFeat:(NSManagedObject *)feat;
-(IBAction)refreshFeatTargetsTable:(id)sender;
-(IBAction)featTargetSheetCancelled:(id)sender;
-(IBAction)featTargetSheetEnded:(id)sender;
-(void)featTargetSheetEnded:(NSWindow *)sheet
				 returnCode:(int)returnCode
					   feat:(NSManagedObject *)feat;
//weapons
-(IBAction)changeWeaponSheetEnded:(id)sender;
-(IBAction)setDefaultWeapon:(id)sender;
-(IBAction)setOffHandWeapon:(id)sender;
-(IBAction)cancelOffHandWeapon:(id)sender;
//spells
-(NSString *)checkTheActiveSpell:(NSManagedObject *)activeEvent;
-(void)setUpCharactersSpellsForChar:(NSManagedObject *)obj
					   forOverwrite:(BOOL)overwrite
					startingAtLevel:(int)startLevel;
-(IBAction)editMetamagicFeats:(id)sender;
-(IBAction)cancelSpellChosen:(id)sender;
-(void)metamagicSheetDidEnd:(NSWindow *)panel
				 returnCode:(int)returnCode
				contextInfo:(NSManagedObject *)tempSpellClass;
-(IBAction)spellChosen:(id)sender;
-(IBAction)spellOverwritePanelCancelled:(id)sender;
-(IBAction)spellOverwritePanelEnded:(id)sender;
-(void)spellOverwritePanelDidEnd:(NSWindow *)panel
					  returnCode:(int)returnCode
					currentSpell:(NSManagedObject *)currentSpell;
-(void)secondHalfOfSpellWithCurrentSpell:(NSManagedObject *)cs
							   overwrite:(BOOL)overwrite;
-(void)runSpellWithCurrentSpell:(NSManagedObject *)currentSpell;
-(NSString *)runningSpellWithCurrentSpell:(NSManagedObject *)currentSpell;
-(void)displaySpellTargetSheetForCurrentSpell:(NSManagedObject *)currentSpell
								  forDuration:(int)duration
							 forDurationLabel:(NSString *)durLabel
									 forRange:(NSString *)range
								forRangeLabel:(NSString *)ranLabel
							   forCustomBlurb:(BOOL)blurb;
-(IBAction)addSpellTarget:(id)sender;
-(IBAction)removeSpellTarget:(id)sender;
-(IBAction)continueFromSpellTargets:(id)sender;
-(IBAction)cancelFromSpellTargets:(id)sender;
-(void)spellTargetPanelDidEnd:(NSWindow *)panel
				   returnCode:(int)returnCode
				 currentSpell:(NSManagedObject *)currentSpell;
-(NSString *)processEffectFromCurrentSpell:(NSManagedObject *)currentSpell
							 onSpellTarget:(NSManagedObject *)spellTarget;
-(BOOL)rollConcentrationForCurrentSpell:(NSManagedObject *)currentSpell;
-(BOOL)rollConcentrationForCharacter:(NSManagedObject *)obj
					   forSpellLevel:(int)spellLevel;
-(void)processSpellsToCastTable;
#pragma mark -
#pragma mark Attacks
#pragma mark
-(NSString *)deathCheck:(NSManagedObject *)obj;
-(void)runAttackFromMainScreenByCharacter:(NSManagedObject *)obj;
-(void)runAttackActions:(BOOL)moveAndAttack
		   forCharacter:(NSManagedObject *)obj;
-(BOOL)usingARangedAttackByChar:(NSManagedObject *)obj
				 mainAttackOnly:(BOOL)moa;
-(IBAction)refreshAttackTargetsTable:(id)sender;
-(void)setUpAttackOOforAction:(NSString *)action;
-(IBAction)attackOOSheetCancelled:(id)sender;
-(IBAction)attackOOSheetEnded:(id)sender;
-(IBAction)changeCover:(id)sender;
-(IBAction)changeImprovedCover:(id)sender;
-(IBAction)changeFlanked:(id)sender;
-(IBAction)checkRangeIsOK:(id)sender;
-(void)getTargetsForCurrentAttack:(NSManagedObject *)currentAttack;
-(IBAction)attackWindowClosed:(id)sender;
-(IBAction)attackWindowCanceled:(id)sender;
-(void)targetForAttackChosen:(NSWindow *)panel
				  returnCode:(int)returnCode
			   currentAttack:(NSManagedObject *)currentAttack;
-(NSString *)runAttackForCurrentAttack:(NSManagedObject *)currentAttack;
-(NSString *)processDamageWithCurrentAttack:(NSManagedObject *)currentAttack
							withCriticalHit:(BOOL)ch
							  withAttackAdj:(int)attackAdj;
/*-(void)grantXPToParty:(int)partyNo
		  forTargetCR:(double)challengeRating;*/
-(int)checkForImmunityOnCharacter:(NSManagedObject *)target
				   withDamageType:(NSString *)damageType
				   withWeaponPlus:(int)plus
					   withDamage:(int)damage;
-(BOOL)aCriticalHitMadeForDieRoll:(int)dieRoll
						forWeapon:(NSManagedObject *)weap
					 forAttackAdj:(int)adj
					  OnCharacter:(NSManagedObject *)obj2;
	//spell discharge.
-(IBAction)cancelSpellDischargeSheet:(id)sender;
-(IBAction)processSpellDischargeSheet:(id)sender;
//more
-(IBAction)startNextRound:(id)sender;
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification;
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex;
- (void)tabView:(NSTabView *)tabView
didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
- (void)windowWillClose:(NSNotification *)aNotification;
//battle exit methods.
-(NSString *)processForExitTheCharacter:(NSManagedObject *)obj
							withMessage:(NSString *)message;
-(NSString *)processActiveEventsForTheMessage:(NSString *)message
							  forTheCharacter:(NSManagedObject *)obj;
-(NSString *)killTheActiveEvent:(NSManagedObject *)event
				  withCharacter:(NSManagedObject *)obj
					withMessage:(NSString *)message2
						 toExit:(BOOL)toExit;
-(void)alertFromCurrentEventDidEnd:(NSWindow *)sheet
			  returnCode:(int)code
			 contextInfo:(NSManagedObject *)currentEvent;
-(IBAction)hitTargetEditPressed:(id)sender;
-(void)processDamageEditFieldsToEnable:(BOOL)toEnable;
-(IBAction)criticalHitsChanged:(id)sender;
-(IBAction)addUpDamageEdit:(id)sender;
-(IBAction)arcaneSpellFailurePressed:(id)sender;
-(IBAction)concentrationCheckPressed:(id)sender;
-(IBAction)passedSpellResistancePressed:(id)sender;
-(IBAction)passedAttackRollPressed:(id)sender;
-(IBAction)targetHasFailedSavingThrowPressed:(id)sender;
-(IBAction)addUpDamageEdit2:(id)sender;
-(IBAction)closeEditSheet:(id)sender;
-(void)editSheetDidEnd:(NSWindow *)sheet
			returnCode:(int)code
		   contextInfo:(NSManagedObject *)currentEvent;
-(void)alertFromEventDidEnd:(NSWindow *)sheet
				 returnCode:(int)code
				contextInfo:(void *)contextInfo;
-(void)alertFromConcCheckDidEnd:(NSWindow *)sheet
					 returnCode:(int)code
					activeEvent:(NSManagedObject *)event;
/*-(void)displayDiceRollSheetForCharacter:(NSManagedObject *)obj
							withMessage:(NSArray *)info;
-(IBAction)diceRollSheetEnded:(id)sender;
-(void)diceRollSheetEnded:(NSWindow *)panel
			   returnCode:(int)returnCode
				  message:(NSArray *)info;*/
@end
